import 'package:flutter/material.dart';
import 'package:module_home/const/string.dart';
import 'package:module_home/model/ISSDataEntity.dart';
import 'package:module_home/model/TransferDataEntity.dart';
import 'package:module_home/widget/IssDetailItem.dart';
import 'package:lib_ui/widget/appbar_widget.dart';
import 'package:lib_navigator/custom_route.dart';

class ISSDetailPage extends StatefulWidget {
  final ChildList childList;

  const ISSDetailPage({Key key, this.childList}) : super(key: key);

  @override
  _ISSDetailPageState createState() => _ISSDetailPageState();
}

class _ISSDetailPageState extends State<ISSDetailPage> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () => _backToData(),
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar: appBar(widget.childList.name, toBack: () {
            _backToData();
          }, actions: <Widget>[
            Padding(
                padding: EdgeInsets.only(right: 15),
                child: Center(
                    child: Container(
                  width: 60,
                  height: 25,
                  child: ElevatedButton(
                    style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Colors.white),
                        shape: MaterialStateProperty.all(StadiumBorder(
                            side: BorderSide(
                          //设置 界面效果
                          // width: 8,
                          style: BorderStyle.none,
                        )))),
                    child: Text(
                      iss_save,
                      style: TextStyle(color: Colors.blue),
                    ),
                    onPressed: () {
                      _backToData();
                    },
                  ),
                )))
          ]),
          body: ListView.builder(
              itemCount: widget.childList.grandChildList.length,
              itemBuilder: (context, index) {
                return IssDetailItem(
                    childList: widget.childList,
                    grandChildList: widget.childList.grandChildList[index]);
              }),
        ));
  }

  //返回并携带数据
  _backToData() {
    List list = [];
    int aisValue = 0;
    widget.childList.grandChildList.forEach((element) {
      if (element.check == 1) {
        list.add(element.value);
        aisValue = aisValue + element.value;
      }
    });
    widget.childList.aisValue = aisValue;
    if (list.length == 0) {
      widget.childList.issValue = 0;
      // print("widget.childList.issValue:" +
      //     widget.childList.issValue.toString() +
      //     ",aisValue" +
      //     widget.childList.aisValue.toString());
    } else {
      list.sort();
      widget.childList.issValue = list.last;
      // print("widget.childList.issValue:" +
      //     widget.childList.issValue.toString() +
      //     ",aisValue" +
      //     widget.childList.aisValue.toString());
    }
    var transferData = TransferDataEntity(
        widget.childList.aisValue, widget.childList.issValue);
    Navigator.pop(context, transferData);
  }
}
